package com.yuxi.Dao.Impl;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.yuxi.Config.UserTableStatic;
import com.yuxi.Dao.EvaluationRecordDao;
import com.yuxi.Entity.EvaluationRecord;
import com.yuxi.Entity.Information;
import com.yuxi.Entity.UserInfo;
import com.yuxi.Request.InformationQuery;
import com.yuxi.Request.Page;
@Repository
public class EvaluationRecordDaoImpl extends BaseDaoImpl<Serializable, Serializable> implements EvaluationRecordDao {
	private Logger logger=LoggerFactory.getLogger(EvaluationRecordDaoImpl.class);
	public EvaluationRecordDaoImpl(){
		super(EvaluationRecord.class);
	}
	@Override
	public EvaluationRecord getRecord(Integer eRecordId) {
		// TODO Auto-generated method stub
		return (EvaluationRecord) super.get(eRecordId);
	}

	@Override
	public EvaluationRecord addRecord(EvaluationRecord eRecord) {
		// TODO Auto-generated method stub
		return (EvaluationRecord) super.addAndReturn(eRecord);
	}

	@Override
	public void deleteRecord(EvaluationRecord eRecord) {
		// TODO Auto-generated method stub
		super.delete(eRecord);
	}

	@Override
	public void modifly(EvaluationRecord eRecord) {
		// TODO Auto-generated method stub
		super.update(eRecord);
	}
	@Override
	public Page<Information> getRateAll(InformationQuery inforQuery,Page<Information> p,int userId) {
		try {
			String  intputUserString="";
			if (inforQuery.getUserId().equals("%")) {
				intputUserString=" ";
			}else{
				intputUserString=" and (inforTable.inputPerSonNames like "+"'%,"+inforQuery.getIntputName()+",%'"+" or inforTable.userId ="+inforQuery.getUserId()+ ") ";
	
			}
			String[] sourceStr;
			String queryBySouce=" ";
			if (inforQuery.getInformationSource().equals("%")) {
				queryBySouce=" ";
			}else{
				sourceStr=inforQuery.getInformationSource().split(",");
				for (int i = 0; i < sourceStr.length; i++) {
					queryBySouce+="  and inforTable.informationSourceID like"+" '%,"+sourceStr[i]+",%'  ";
				}
			}//+" "+queryBySouce
			String port;
			if (inforQuery.getSalfPort().equals("%")) {
				port="  ";
			}else{
				port=" and inforTable.safetyPortId in("+inforQuery.getSalfPort()+") ";
			}
			String  str = "new  com.yuxi.Entity.Information(inforTable.id, inforTable.informationAccount, inforTable.informationType, inforTable.informationSourceID, inforTable.informationSourceNames, inforTable.inputPersonID, inforTable.inputPerSonNames, inforTable.informationProfile, inforTable.informationConternt, inforTable.mesure, inforTable.standard, inforTable.informationClassificationId, inforTable.informationSortId, inforTable.informationClassificationNames, inforTable.informationSortNames, inforTable.appendix, inforTable.taskCompletion, inforTable.safetyPortId, inforTable.safetyPortNames, inforTable.createTime, inforTable.userId, inforTable.jbpmPdId, inforTable.flowId, inforTable.flowName, inforTable.prisonId)";
			
			
			//"SELECT COUNT(id) from Information information where (information.flowId<? and information.id not in(SELECT info.id from Information info,CommentRecord record where record.informationId=info.id) or information.flowId=?) AND information.prisonId=?
			String hql = "select DISTINCT " + str+ " from Information inforTable,EvaluationRecord record where record.informationId=inforTable.id and record.userid= ? and inforTable.createTime BETWEEN ? AND ? "
					+ "and  inforTable.informationSortId like ? and  inforTable.informationClassificationId like ? and inforTable.informationProfile like ? and inforTable.flowId like ? and inforTable.informationClassificationId like ?  "
					+intputUserString+port+" and inforTable.prisonId like ? "+" "+queryBySouce+ " order By inforTable.createTime desc ";
			String noSelct = " from Information inforTable,EvaluationRecord record where record.informationId=inforTable.id and record.userid= ? and inforTable.createTime BETWEEN ? AND ? "
					+ "and  inforTable.informationSortId like ? and  inforTable.informationClassificationId like ? and inforTable.informationProfile like ? and inforTable.flowId like ? and inforTable.informationClassificationId like ?  "
					+intputUserString+port+" and inforTable.prisonId like ? "+" "+queryBySouce+ " order By inforTable.createTime desc ";
	
			p = super.splitPageQuerySlect(hql, p,noSelct,userId, inforQuery.getStartTimestamp(), inforQuery.getEndTimestamp(),
					inforQuery.getInformationSort(), inforQuery.getInformationLv(),
					"%" + inforQuery.getKey() + "%",
					inforQuery.getCurFLow(), inforQuery.getInformationType()
						,inforQuery.getStructure() );
			logger.info("查询信息成功");
					
				
			
			List<Information> informations=p.getList();
			for (Iterator iterator = informations.iterator(); iterator.hasNext();) {
				Information information = (Information) iterator.next();
				
				information.setUserName(UserTableStatic.findUserById(Integer.valueOf(information.getUserId())).getUserName());
			}
			p.setList(informations);
			return p;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询信息失败");
			return null;
		}
	}
	
	@Override
	public EvaluationRecord getEvaluationRecordByInfoId(Integer InfoId,Integer userId) {
		return (EvaluationRecord)super.find("from EvaluationRecord where informationId=? and userId=? order by createTime desc",InfoId,userId).get(0);
	}
	@Override
	public List<EvaluationRecord> getAllEvaluationRecord(Integer InfoId) {
		// TODO Auto-generated method stub
		return super.find("from EvaluationRecord where informationId=? order by createTime desc",InfoId);

	}

}
